﻿using AIStudio.Core;
using AIStudio.Core.ViewModels;
using AIStudio.Wpf.Business.Bussiness;
using AIStudio.Wpf.Business.IBussiness;
using AIStudio.Wpf.Client.Views;
using AIStudio.Wpf.Home.Views;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using AIStudio.Core.Models;

namespace AIStudio.Wpf.Client.ViewModels
{
    public class LoginViewModel : TitleViewModel
    {
        IContainerExtension _container;
        IRegionManager _regionManager;
        IDataProvider _dataProvider;
        public LoginViewModel(IContainerExtension container, IRegionManager regionManager, IDataProvider dataProvider)
        {
            _container = container;
            _regionManager = regionManager;
            //_dataProvider = new ApiDataProvider();
            _dataProvider = dataProvider;

            Title = "Login";
        }

        private string _serverIP = LocalSetting.ServerIP;
        public string ServerIP
        {
            get { return _serverIP; }
            set
            {
                SetProperty(ref _serverIP, value);
            }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set
            {
                SetProperty(ref _userName, value);
            }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                SetProperty(ref _password, value);
          
            }
        }

        private bool _isRmembered = true;
        public bool IsRmembered
        {
            get { return _isRmembered; }
            set
            {
                SetProperty(ref _isRmembered, value);
            }
        }

        private ICommand _loginCommand;
        public ICommand LoginCommand
        {
            get
            {
                return this._loginCommand ?? (this._loginCommand = new DelegateCommand<Window>(para => this.Login(para)));
            }
        }

      

        private async void Login(Window window)
        {
            if (!string.IsNullOrEmpty(UserName)  && !string.IsNullOrEmpty(Password))
            {
                var mD5Password = Password.ToMD5String();
                var token = await _dataProvider.GetToken(ServerIP, UserName, mD5Password);
                if (!token.Success)
                {
                    MessageBox.Show(token.Msg);
                    return;
                }

                window.DialogResult = true;
                window.Close();

                //_regionManager.RegisterViewWithRegion("MainContentRegion", nameof(IntroduceView));
            }
            else
            {
                MessageBox.Show("请输入用户名或密码！");
            }


        }

    }
}
